class MongoDB {
    constructor (collection) {
        this.collection = collection
    }

    async add (doc) {
        Array.isArray(doc) ? this.isOne = false : this.isOne = true
        if (this.isOne) {
            const record = new this.collection(doc)
            await record.save()
        } else {
            await this.collection.insertMany(doc)
        }
        return true
    }

    async get (filter, max = 20) {
        return await this.collection.find(filter).limit(max);
    }

    async getAll(filter){
        return await this.collection.find(filter);
    }

    async remove (filter) {
        const data = await this.collection.deleteMany(filter)
        return data.deletedCount
    }

    async update (filter, updated) {
        const data = await this.collection.updateMany(filter, updated)
        return data.modifiedCount
    }
}

module.exports = MongoDB
